package com.wuye.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wuye.common.util.bean.EntityCopyUtil;
import com.wuye.common.util.numeric.NumericUtil;
import com.wuye.common.util.string.StrUtil;
import com.wuye.common.vo.PageInfo;
import com.wuye.common.vo.RetVO;
import com.wuye.constants.BaseConstants;
import com.wuye.entity.Address;
import com.wuye.entity.Community;
import com.wuye.entity.Organization;
import com.wuye.entity.PartyInfo;
import com.wuye.entity.Privilege;
import com.wuye.entity.PropertyCompany;
import com.wuye.entity.Role;
import com.wuye.entity.User;
import com.wuye.services.UserServiceManager;
import com.wuye.services.vo.TreeNodeVo;
import com.wuye.util.ExcelUtil;

public class UserAction extends ActionSupport{
	private static Logger log = Logger.getLogger(UserAction.class);
	
	@Autowired
	private UserServiceManager userServiceManager;
	@Setter
	@Getter
	private String comingParams;
	@Setter
	@Getter
	private String suc_info = "";
	@Setter
	@Getter
	private int page;
	@Setter
	@Getter
	private Object result;
	@Setter
	@Getter
	private Object input;
	@Setter
	@Getter
	private File fileupload;//这里的"fileName"一定要与表单中的文件域名相同    
	@Setter
	@Getter
    private String fileuploadContentType;//格式同上"fileName"+ContentType
	@Setter
	@Getter
    private String fileuploadFileName;//格式同上"fileName"+FileName    
	
	public String saveUser() {
		Map<String,Object> map = new HashMap<String, Object>();
		Map<String,Object> inMap = new HashMap<String, Object>();
		try {
			JSONObject json =JSONObject.fromObject(comingParams);
			String type = StrUtil.strnull(json.get("type"));
			
			JSONArray jsonArray = json.getJSONArray("data");
			if (jsonArray != null && jsonArray.size() > 0){
				inMap.put("user", jsonArray);
				inMap.put("type", type);
				RetVO retVo = userServiceManager.save(inMap);
				if (BaseConstants.RET_TRUE.equals(retVo.getResult())){
					map.put("result", "true");
					map.put("msg", "保存小区信息成功");
				}else {
					map.put("result", "false");
					map.put("msg", "保存小区信息失败");
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "false");
			map.put("msg", "保存信息失败");
			log.info(e.getMessage());
		}
		JSONObject jsontip = JSONObject.fromObject(map);
		suc_info = jsontip.toString();
		return SUCCESS;
	}
	
	public String getOrganizationByUser() {
		User user = (User) ActionContext.getContext().getSession().get("user");
		Map<String, Object> map = new HashMap<String, Object>();
		if (BaseConstants.USER_TYPE_11.equals(user.getUserType())) {  //物业用户
			map.put("propertyCompany", new PropertyCompany());
			map.put("community", new Community());
		} else {
			map.put("isSys", "yes");
		}
		JSONObject json = JSONObject.fromObject(map);
		suc_info = json.toString();
		return "organization_info";
	}
	
	/**
	 * 获取修改的用户的团队信息
	 * @return
	 */
	public String getOrgs(){
		Map<String,Object> retMap = new HashMap<String, Object>();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		try {
			JSONObject json =JSONObject.fromObject(comingParams);
			
			if (!StrUtil.isNullOrEmpty(json.get("companyId"))){
				paramMap.put("companyId", StrUtil.strnull(json.get("companyId")));
				
				String type = StrUtil.strnull(json.get("qryType"));
				RetVO retvo = userServiceManager.getOrgs(paramMap);
				List<Organization> allOrgs = (List<Organization>)retvo.getObject();
				paramMap.clear();
				List<Organization> existOrgs = null;//所修改的用户已有团队信息
				if (!StrUtil.isNullOrEmpty(json.get("userId")) && !"\"null\"".equals(json.get("userId"))){
					paramMap.put("userId", json.getInt("userId"));
					retvo = userServiceManager.getOrgs(paramMap);
					existOrgs = (List<Organization>)retvo.getObject();
					
				}
				//User user = (User) ActionContext.getContext().getSession().get("user");
				//paramMap.put("userId", user.getId());
				
				if (allOrgs != null && allOrgs.size() > 0){
					List<TreeNodeVo> retNodes = new ArrayList<TreeNodeVo>();
					for (Organization allOrg : allOrgs){
						TreeNodeVo vo = new TreeNodeVo();
						vo.setId(allOrg.getId());
						if (StrUtil.isNullOrEmpty(allOrg.getUpOrgId())){
							vo.setpId(-1);
						}else{
							vo.setpId(allOrg.getUpOrgId());
						}
						if (existOrgs != null && existOrgs.size() > 0){
							for (Organization existOrg : existOrgs){
								if (allOrg.getId().equals(existOrg.getId())){
									vo.setChecked(true);
									break;
								}
							}
						}
						vo.setName(allOrg.getOrgName());
						retNodes.add(vo);
						
					}
					retMap.put("data", retNodes);
					
				}
				retMap.put("result", "true");
				retMap.put("msg", "查询成功!");
			}else{
				retMap.put("result", "true");
				retMap.put("msg", "查询成功!");
				
			}
		} catch (Exception e) {
			log.info("异常了", e);
			retMap.put("result", "false");
			retMap.put("msg", "查询失败!");
		}
		/*JsonConfig jsonConfig = SpringUtil.getBean("jsonConfig");
		
		//jsonConfig.setJsonPropertyFilter(new InvisibleFilter(true,"no")); 
		//jsonConfig.setJsonPropertyFilter(new HibernatePropertyFilter()); 
		//jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
		jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonDateValueProcessor("yyyy-MM-dd hh:mm:ss"));  
		*/
		JSONObject jsonArr = JSONObject.fromObject(retMap);
		suc_info = jsonArr.toString();
		return SUCCESS;
	}
	
	public String getUser(){
		Map<String,Object> retMap = new HashMap<String, Object>();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		try {
			JSONObject json =JSONObject.fromObject(comingParams);
			if(!StrUtil.isNullOrEmpty(json.get("account"))){
				paramMap.put("account", NumericUtil.nullToIntegerZero(json.get("account")));
			}
			if (!StrUtil.isNullOrEmpty(json.get("companyName"))){
				paramMap.put("companyName", StrUtil.strnull(json.get("companyName")));
			}
			
			if (!StrUtil.isNullOrEmpty(json.get("partyName"))){
				paramMap.put("partyName", StrUtil.strnull(json.get("partyName")));
			}
			if (!StrUtil.isNullOrEmpty(json.get("userId"))){
				paramMap.put("userId", StrUtil.strnull(json.get("userId")));
			}
			/*if (!StrUtil.isNullOrEmpty(json.get("communityName"))){
				paramMap.put("communityName", StrUtil.strnull(json.get("communityName")));
			}*/
			if (!StrUtil.isNullOrEmpty(json.get("qryType"))){
				paramMap.put("qryType", StrUtil.strnull(json.get("qryType")));
			}
			RetVO retvo = null ;
			/*if(type.equals("getCompanySimple")){
				retvo = propertyCompanyServiceManager.getCommunitySimple(paramMap);
			}*/
			retvo = userServiceManager.getUser(paramMap,Integer.valueOf(page), BaseConstants.DEFAULT_PAGE_PER_COUNT);
			if (retvo != null && BaseConstants.RET_TRUE.equals(retvo.getResult())){
				if (retvo.getObject() != null){
					JSONArray jsArray = (JSONArray)retvo.getObject();
					suc_info = jsArray.toString();
					retMap.put("data", jsArray);
				}
				retMap.put("result", "true");
				retMap.put("msg", "信息查询成功!");
				
			}else{
				retMap.put("result", "false");
				retMap.put("msg", "信息查询失败!");
			}
			
			//JsonConfig jsonConfig = new JsonConfig();
			
			
		} catch (Exception e) {
			e.printStackTrace();
			retMap.put("result", "false");
			retMap.put("msg", "信息查询失败!");
		}
		/*JsonConfig jsonConfig = SpringUtil.getBean("jsonConfig");
		
		//jsonConfig.setJsonPropertyFilter(new InvisibleFilter(true,"no")); 
		//jsonConfig.setJsonPropertyFilter(new HibernatePropertyFilter()); 
		//jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
		jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonDateValueProcessor("yyyy-MM-dd hh:mm:ss"));  
		*/
		JSONObject jsonArr = JSONObject.fromObject(retMap);
		suc_info = jsonArr.toString();
		return SUCCESS;
	}
	
	/**
	 * 获取客户信息.
	 * @return
	 * @author Luxb
	 * 2015-12-19 Luxb
	 */
	public String qryUserList() {
	    Map<String,Object> retMap = new HashMap<String, Object>();
        Map<String,Object> paramMap = new HashMap<String, Object>();
        try {
            JSONObject json =JSONObject.fromObject(comingParams);
            if(!StrUtil.isNullOrEmpty(json.get("account"))){
                paramMap.put("account", json.get("account"));
            }
            if (!StrUtil.isNullOrEmpty(json.get("companyName"))){
                paramMap.put("companyName", StrUtil.strnull(json.get("companyName")));
            }
            
            if (!StrUtil.isNullOrEmpty(json.get("partyName"))){
                paramMap.put("partyName", StrUtil.strnull(json.get("partyName")));
            }
            if (!StrUtil.isNullOrEmpty(json.get("qryType"))){
                paramMap.put("qryType", StrUtil.strnull(json.get("qryType")));
            }
            User qryUser = (User)ActionContext.getContext().getSession().get("user");
            paramMap.put("qryUser", qryUser);
            if (qryUser == null){
            	retMap.put("result", "false");
                retMap.put("msg", "信息查询失败!");
                JSONObject jsonArr = JSONObject.fromObject(retMap);
                suc_info = jsonArr.toString();
                return SUCCESS;
            }
            Object companyId = ActionContext.getContext().getSession().get("companyId");
            if (companyId != null){
            	paramMap.put("companyId", (Integer)companyId);
            }
            Object communityId = ActionContext.getContext().getSession().get("communityId");
            if (communityId != null){
            	paramMap.put("communityId", (Integer)communityId);
            }
            Object orgId = ActionContext.getContext().getSession().get("orgId");
            if (orgId != null){
            	paramMap.put("orgId", (Integer)orgId);
            }
            PageInfo pageInfo = userServiceManager.qryUserList(paramMap, page, 12);
            JSONObject pg = new JSONObject();
            if (pageInfo != null) {
                pg.put("perPageCount", pageInfo.getPerPageCount());
                pg.put("totalPageCount", pageInfo.getTotalPageCount());
                pg.put("currentPage", pageInfo.getCurrentPage());
                pg.put("totalCount", pageInfo.getTotalCount());
                List<Object> dataList = pageInfo.getDataList();
                JSONArray jsArray = new JSONArray();
                if (dataList != null && dataList.size() > 0){
                    for (Object obj : dataList){
                        User user = (User)obj;
                        PartyInfo partyInfo = user.getPartyInfo() ;
                        JSONObject jsobj = EntityCopyUtil.getJSONObject(user,null);
                        if (partyInfo != null){
                        	jsobj.put("partyInfo", EntityCopyUtil.getJSONObject(partyInfo,null));
                        	Address address = partyInfo.getAddress();
                            PropertyCompany company = user.getOwnerCompany();
                            jsobj.put("company", EntityCopyUtil.getJSONObject(company,null));
                            jsobj.put("address", EntityCopyUtil.getJSONObject(address,null));
                        }
                        
                        jsArray.add(jsobj);
                        pg.put("dataList", jsArray);
                    }
                }
            }
            
            retMap.put("pageInfo", pg);
            retMap.put("result", "true");
            retMap.put("msg", "用户查询查询成功!");
        } catch (Exception e) {
            retMap.put("result", "false");
            retMap.put("msg", "信息查询失败!");
        }
        JSONObject jsonArr = JSONObject.fromObject(retMap);
        suc_info = jsonArr.toString();
        return SUCCESS;
	}
	/**
	 * 用户删除.
	 * @return
	 * @author Luxb
	 * 2015-12-19 Luxb
	 */
	public String removeUser() {

        Map<String,Object> map = new HashMap<String, Object>();
        Map<String,Object> inMap = new HashMap<String, Object>();
        try {
            if (comingParams != null) {
                inMap.put("userIdArray", JSONArray.fromObject(comingParams));
                
                RetVO retVo = userServiceManager.del(inMap);
                if (BaseConstants.RET_TRUE.equals(retVo.getResult())){
                    map.put("result", "true");
                    map.put("msg", "用户删除成功！");
                }else {
                    map.put("result", "false");
                    map.put("msg", "用户删除失败！");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", "false");
            map.put("msg", "用户删除失败！");
            log.info(e.getMessage());
        }
        JSONObject jsontip = JSONObject.fromObject(map);
        suc_info = jsontip.toString();
        return SUCCESS;
	}
	
	/**
	 * 取修改用户的角色信息
	 * @return
	 */
	public String getRoles(){
		Map<String,Object> retMap = new HashMap<String, Object>();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		try {
			JSONObject json =JSONObject.fromObject(comingParams);
			//当前登录用户的角色 
			User user = (User)ActionContext.getContext().getSession().get("user");
			//List<Role> allRoles = user.getRoles();
			paramMap.put("userId", user.getId());
			RetVO retvo = userServiceManager.getRoles(paramMap);
			JSONArray allRoles = (JSONArray)retvo.getObject();
			JSONArray existRoles = null;//所修改的用户已有角色信息
			String qryType = StrUtil.strnull(json.get("qryType"));
			if (!StrUtil.isNullOrEmpty(json.get("userId"))){
				paramMap.clear();
				paramMap.put("userId", json.getInt("userId"));
				retvo = userServiceManager.getRoles(paramMap);
				existRoles = (JSONArray)retvo.getObject();
			}
			List<TreeNodeVo> retNodes = new ArrayList<TreeNodeVo>();
			if ("u".equals(qryType)){
				//只查当前用户的角色信息
				
				if (existRoles != null && existRoles.size() > 0){
					for (int j=0; j < existRoles.size(); j++){
						TreeNodeVo vo = new TreeNodeVo();
						vo.setId(existRoles.getJSONObject(j).getInt("roleId"));
						vo.setName(existRoles.getJSONObject(j).getString("roleName"));
						vo.setChecked(true);
						retNodes.add(vo);
					}
					retMap.put("data", retNodes);
				}
			}else{
				
				if (allRoles != null && allRoles.size() > 0){
					for (int i=0; i < allRoles.size(); i++){
						TreeNodeVo vo = new TreeNodeVo();
						vo.setId(allRoles.getJSONObject(i).getInt("roleId"));
						vo.setName(allRoles.getJSONObject(i).getString("roleName"));
						if (existRoles != null && existRoles.size() > 0){
							for (int j=0; j < existRoles.size(); j++){
								if (allRoles.getJSONObject(i).getString("roleId").equals(existRoles.getJSONObject(j).getString("roleId"))){
									vo.setChecked(true);
									break;
								}
							}
						}
						
						retNodes.add(vo);
						
					}
					retMap.put("data", retNodes);
				}
			}
			
			retMap.put("result", "true");
			retMap.put("msg", "查询成功!");
		} catch (Exception e) {
			log.info("异常了", e);
			retMap.put("result", "false");
			retMap.put("msg", "查询失败!");
		}
		JSONObject jsonArr = JSONObject.fromObject(retMap);
		suc_info = jsonArr.toString();
		return SUCCESS;
	}

	public String hasPrivilege(){
		Map<String,Object> retMap = new HashMap<String, Object>();
		try {
			JSONArray json = JSONArray.fromObject(comingParams);
			if (json != null || json.size() > 0){
				String[] privilegeCode = new String[json.size()];
				for (int i = 0; i < json.size(); i++){
					privilegeCode[i] = json.getString(i);
				}
				//当前登录用户
				User user = (User)ActionContext.getContext().getSession().get("user");

				RetVO ret = userServiceManager.hasPrivilege(user, privilegeCode);
				if (ret.getObject() != null){
					JSONArray jsarry = (JSONArray)ret.getObject();
					retMap.put("data", jsarry);
				}
			}
			retMap.put("result", "true");
			retMap.put("msg", "查询成功!");
		} catch (Exception e) {
			log.info("异常了", e);
			retMap.put("result", "false");
			retMap.put("msg", "查询失败!");
		}
		JSONObject jsonArr = JSONObject.fromObject(retMap);
		suc_info = jsonArr.toString();
		return SUCCESS;
		
	}
	
	/**
	 * 获取权限设置的权限信息
	 * @return
	 */
	public String getPrivilege(){
		Map<String,Object> retMap = new HashMap<String, Object>();
		Map<String,Object> paramMap = new HashMap<String, Object>();
		try {
			JSONObject json =JSONObject.fromObject(comingParams);
			//当前登录用户的角色 
			User user = (User)ActionContext.getContext().getSession().get("user");
			//List<Role> allRoles = user.getRoles();
			paramMap.put("userId", user.getId());
			//paramMap.put("isDirect", "1");
			
			RetVO retvo = userServiceManager.getPrivileges(paramMap);
			List<Privilege> allPrivileges = (List<Privilege>)retvo.getObject();
			List<Privilege> existPrivileges = null;//所修改的用户已有角色信息
			String qryType = StrUtil.strnull(json.get("qryType"));
			if (!StrUtil.isNullOrEmpty(json.get("userId"))){
				paramMap.clear();
				paramMap.put("userId", json.getInt("userId"));
				paramMap.put("isDirect", "1");
				retvo = userServiceManager.getPrivileges(paramMap);
				existPrivileges = (List<Privilege>)retvo.getObject();
			}
			List<TreeNodeVo> retNodes = new ArrayList<TreeNodeVo>();
			if ("u".equals(qryType)){
				//只查当前用户的角色信息
				if (existPrivileges != null && existPrivileges.size() > 0){
					for (int j=0; j < existPrivileges.size(); j++){
						TreeNodeVo vo = new TreeNodeVo();
						vo.setId(existPrivileges.get(j).getPrivilegeId());
						vo.setName(existPrivileges.get(j).getPrivilegeName());
						vo.setChecked(true);
						retNodes.add(vo);
					}
					retMap.put("data", retNodes);
				}
			}else{
				
				if (allPrivileges != null && allPrivileges.size() > 0){
					for (int i=0; i < allPrivileges.size(); i++){
						TreeNodeVo vo = new TreeNodeVo();
						vo.setId(allPrivileges.get(i).getPrivilegeId());
						vo.setName(allPrivileges.get(i).getPrivilegeName());
						if (StrUtil.isNullOrEmpty(allPrivileges.get(i).getParentPrivilegeId())){
							vo.setpId(-1);
						}else{
							vo.setpId(allPrivileges.get(i).getParentPrivilegeId());
						}
						if (existPrivileges != null && existPrivileges.size() > 0){
							for (int j=0; j < existPrivileges.size(); j++){
								if (allPrivileges.get(i).getPrivilegeId().equals(existPrivileges.get(j).getPrivilegeId())){
									vo.setChecked(true);
									break;
								}
							}
						}
						
						retNodes.add(vo);
						
					}
					retMap.put("data", retNodes);
				}
			}
			
			retMap.put("result", "true");
			retMap.put("msg", "查询成功!");
		} catch (Exception e) {
			log.info("异常了", e);
			retMap.put("result", "false");
			retMap.put("msg", "查询失败!");
		}
		JSONObject jsonArr = JSONObject.fromObject(retMap);
		suc_info = jsonArr.toString();
		return SUCCESS;
	}
	
	public String uploadFile(){
		Map<String,Object> retMap = new HashMap<String, Object>();
		Map<String,Object> inMap = new HashMap<String, Object>();
		
		if (fileupload != null){
			try {
				RetVO userRet = this.checkImport(fileupload);
				if (BaseConstants.RET_TRUE.equals(userRet.getResult())){
					JSONArray userArray = (JSONArray)userRet.getObject();
					inMap.put("user", userArray);
					RetVO retVo = userServiceManager.save(inMap);
					if (BaseConstants.RET_TRUE.equals(retVo.getResult())){
						retMap.put("result", "true");
						retMap.put("msg", "保存信息成功");
					}else {
						retMap.put("result", "false");
						retMap.put("msg", "保存信息失败");
					}
				}else{
					retMap.put("result", "false");
					retMap.put("msg", userRet.getRetMsg());
				}
			} catch (Exception e) {
				log.info(e.getMessage());
				retMap.put("result", "false");
				retMap.put("msg", "保存失败");
			} 
		}
		JSONObject jsonArr = JSONObject.fromObject(retMap);
		suc_info = jsonArr.toString();
		return SUCCESS;
	}

	private RetVO checkImport(File file) {
		RetVO retVo = RetVO.newInstance(BaseConstants.RET_TRUE,"");
		try {
			String[][] dataArray = ExcelUtil.readExcel(file);
			if (dataArray == null || dataArray.length==0){
				retVo.setResult(BaseConstants.RET_FALSE);
				retVo.setRetMsg("导入的文件内容为空!");
				return retVo;
			}
			JSONArray userArray = new JSONArray();
			Set<String> nbrSet = new HashSet<String>();	//导入的号码列表
			for (int i = 0; i < dataArray.length; i++) {
	            String account = StrUtil.strTrim(dataArray[i][0]);
	            String partyName = StrUtil.strTrim(dataArray[i][1]);
	            String detailAddress = StrUtil.strTrim(dataArray[i][2]);
	            String companyName = StrUtil.strTrim(dataArray[i][3]);
	            String orgName = StrUtil.strTrim(dataArray[i][4]);
	            String roleName = StrUtil.strTrim(dataArray[i][5]);
	            
	            if ("".equals(account) || "".equals(partyName)){
	            	retVo.setResult(BaseConstants.RET_FALSE);
					retVo.setRetMsg("手机号和名称不可空");
					return retVo;
	            }
	            if (nbrSet.contains(account)) {
	            	retVo.setResult(BaseConstants.RET_FALSE);
					retVo.setRetMsg(String.format("文件中的手机号[s%]重复", account));
					return retVo;
	            }
	            PropertyCompany company;
	            User user = (User)ActionContext.getContext().getSession().get("user");
	            if (BaseConstants.USER_TYPE_10.equals(user.getUserType())){
	            	if ("".equals(companyName)){
	            		retVo.setResult(BaseConstants.RET_FALSE);
						retVo.setRetMsg("物业公司名称不能为空");
						return retVo;
	            	}
	            	company = PropertyCompany.getDao().getCompanyByName(companyName);
	            	/*if (company == null){
		            	retVo.setResult(BaseConstants.RET_FALSE);
						retVo.setRetMsg(String.format("物业公司[s%]不存在", companyName));
						return retVo;
		            }*/
	            }else{
	            	company = user.getOwnerCompany();
	            }
	            if (company == null){
	            	retVo.setResult(BaseConstants.RET_FALSE);
					retVo.setRetMsg("物业公司不存在");
					return retVo;
	            }
	            
	            Organization org = null;
	            if (!"".equals(orgName)){
	            	org =Organization.getDao().getOrgByOrgTypeAndName(company, BaseConstants.ORG_TYPE_COMMUNITY, orgName);
		            if (org == null){
		            	retVo.setResult(BaseConstants.RET_FALSE);
						retVo.setRetMsg(String.format("小区[s%]不存在", orgName));
						return retVo;
		            }
	            }
	            
	            List<Role> roles = new ArrayList<Role>();
	            if (!"".equals(roleName)){
	            	String[] roleNames = roleName.split(",");
	            	if (roleNames != null && roleNames.length > 0){
	            		for (String eachName:roleNames){
	            			Role role = Role.getDao().getRole(eachName);
	            			if (role == null){
	            				retVo.setResult(BaseConstants.RET_FALSE);
	        					retVo.setRetMsg(String.format("角色[s%]不存在", eachName));
	        					return retVo;
	            			}
	            			roles.add(role);
	            		}
	            	}
	            }
	            
	            nbrSet.add(account);
            	JSONObject userjs = new JSONObject();
            	JSONObject partyInfo = new JSONObject();
            	JSONObject address = new JSONObject();
            	partyInfo.put("partyName", partyName);
            	address.put("detailAddress", detailAddress);
            	partyInfo.put("address", address);
            	
            	JSONObject propertyCompany = new JSONObject();
            	propertyCompany.put("companyId", company.getId());
            	
            	JSONArray userOrgRel = new JSONArray();
            	JSONObject userOrgRelObj = new JSONObject();
            	
            	//设置物业公司的单位关联
            	userOrgRelObj.put("orgId", company.getOrganization().getOrgId());
            	userOrgRel.add(userOrgRelObj);
            	
            	if (org != null){
            		JSONObject userOrgRelObj2 = new JSONObject();
            		userOrgRelObj.put("orgId", org.getId());
                	userOrgRel.add(userOrgRelObj2);
            	}
            	
            	JSONArray userAuth = new JSONArray();
            	if (roles != null && roles.size() > 0){
            		for (Role eachRole : roles){
            			JSONObject userAuthOjb = new JSONObject();
            			userAuthOjb.put("roleId", eachRole.getId());
            			userAuth.add(userAuthOjb);
            		}
            	}
            	
            	userjs.put("account", account);
            	userjs.put("userType", BaseConstants.USER_TYPE_11);
            	userjs.put("statusCd", BaseConstants.STATUS_NOT_EFF);
            	userjs.put("partyInfo", partyInfo);
            	userjs.put("propertyCompany", propertyCompany);
            	userjs.put("userAuth", userAuth);
            	userjs.put("userOrgRel", userOrgRel);
            	
            	userArray.add(userjs);
	        }
			retVo.setObject(userArray);
		} catch (Exception e) {
			retVo.setResult(BaseConstants.RET_FALSE);
			retVo.setRetMsg("导入的文件时出错!");
		}
		
		return retVo;
	}
}
